package com.zq.learn.springbatch.mongo.sourcedata;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * SourceDataService
 *
 * @author ZhangQi
 * @date 2020/2/13
 */
@Service
public class SourceDataService {
  @Autowired
  private MongoTemplate mongoTemplate;

  /**
   * 生成测试数据,由测试类触发。
   */
  public void insertData(){
    List<SourceData> dataList=new ArrayList<>();
    for(int i=0;i<100;i++) {
      int price = new Random().nextInt(500) + 1;
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
      int status = price % 2;
      SourceData order =
        new SourceData(UUID.randomUUID().toString(), price, sdf.format(new Date()), status);
      dataList.add(order);
    }
    mongoTemplate.insertAll(dataList);
  }
}
